package com.github.freewu.commons.libs;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by free on 16-9-23.
 */

public class LoadingView extends View implements ValueAnimator.AnimatorUpdateListener {
    private int numbers = 12;

    protected Path[] paths = new Path[numbers];

    private Paint paint;

    private int paintColor=Color.WHITE;

    private boolean isInit = false;

    protected int width;
    protected int height;

    private int[] alphas = new int[numbers];

    private ValueAnimator animator;

    public static final float SHORT=3.8f;
    public static final float NORMAL=3.2f;
    public static final float LONG=2.5f;

    public float pathLength=NORMAL;

    public LoadingView(Context context) {
        super(context);

        init();
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void initPaths() {
        for (int i = 0; i < numbers; i++) {
            Path path = new Path();
            path.moveTo(0, height / 10);

            addTopArc(path);

            path.lineTo(height / 10, height / pathLength);

            addBottomArc(path);

            path.lineTo(0, height / 20);

            Matrix matrix = new Matrix();
            matrix.postScale(0.5f, 0.5f);
            matrix.postTranslate(width / 2 - height / 30, height / 8);
            matrix.postRotate((360 / numbers) * i, width / 2, height / 2);

            path.transform(matrix);

            paths[i] = path;
        }
    }
    private void init() {
        animator = ValueAnimator.ofInt(0, 255);
        animator.setDuration(1000);
        animator.setInterpolator(null);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(this);

        for (int i = 0; i < numbers; i++) {
            alphas[i] = 255;
        }

        animator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        width = getMeasuredWidth();

        height = getMeasuredHeight();

        if (!isInit) {
            initPaths();

            isInit = true;
        }

        for (int i = 0; i < paths.length; i++) {
            canvas.drawPath(paths[i], getPaint(alphas[i]));
        }
    }

    private Paint getPaint(int alpha) {
        if (paint==null) {
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(1);
            paint.setColor(Color.BLACK);
            paint.setTextSize(20);
            paint.setColor(paintColor);
        }
        paint.setAlpha(alpha);

        return paint;
    }

    private void addTopArc(Path path) {
        RectF rectF = new RectF(0, 0, height / 10, height / 10);

        path.addArc(rectF, 0, -180);
    }

    private void addBottomArc(Path path) {
        RectF rectF = new RectF(0, height / pathLength - height / 20, height / 10, (height / 20) + height / pathLength);

        path.addArc(rectF, 0, 180);
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int alpha = (int) animation.getAnimatedValue();

        for (int i = 0; i < alphas.length; i++) {

            int setAlpha = alpha + (int) (((float) i / numbers) * 255);

            if (setAlpha > 255) {
                alphas[i] = setAlpha - 255;
            } else {
                alphas[i] = setAlpha;
            }
        }

        this.invalidate();
    }

    /**
     * 开始动画
     */
    public void start(){
        animator.start();
    }

    /**
     * 关闭动画
     */
    public void cancel(){
        animator.cancel();
    }

    public void setPaintColor(int paintColor) {
        if (paint==null) {
            this.paintColor = paintColor;
        }else{
            paint.setColor(paintColor);
        }
    }

    public float getPathLength() {
        return pathLength;
    }

    public void setPathLength(float pathLength) {
        this.pathLength = pathLength;
    }
}
